package com.ruoyi.wisdom.event;

import com.ruoyi.wisdom.domain.IotPickScene;
import com.ruoyi.wisdom.domain.swap.SwapPickScene;
import com.ruoyi.wisdom.service.IIotPickGroupService;
import com.ruoyi.wisdom.service.IIotPickSceneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class PickSceneEvent extends IotEvent {

    @Autowired
    private IIotPickSceneService iotPickSceneServic;

    @Autowired
    private IIotPickGroupService iotPickGroupService;

    /**
     * 处理
     *
     * @param swapPickScene
     */
    public void handle(SwapPickScene swapPickScene) {
        if (swapPickScene.getOperate() == null) {
            save(swapPickScene);
        } else {
            switch (swapPickScene.getOperate()) {
                case "delete":
                case "remove":
                    delete(swapPickScene);
                    break;
                default:
                    save(swapPickScene);
            }
        }
    }

    /**
     * 添加|编辑
     *
     * @param swapPickScene
     */
    public void save(SwapPickScene swapPickScene) {
        IotPickScene result = iotPickSceneServic.selectIotPickSceneById(swapPickScene.getId());
        // 实体装换
        IotPickScene iotPickScene = getModelMapper().map(swapPickScene, IotPickScene.class);
        if (result != null) {
            iotPickScene.setId(swapPickScene.getId());
            iotPickSceneServic.updateIotPickScene(iotPickScene);
        } else {
            iotPickSceneServic.insertIotPickScene(iotPickScene);
        }
    }

    /**
     * 删除
     *
     * @param swapPickScene
     */
    public void delete(SwapPickScene swapPickScene) {
        iotPickSceneServic.deleteIotPickSceneById(swapPickScene.getId());
    }
}
